// pages/index/index.js
import request from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    categoryList: [],
    goodsList: [],
    pageNum: 1,
    pageSize: 10,
    isShowMask: true,
    notice:"优惠规则调整（试运行）：商品促销活动（满减、满件减、N元任选、第二件特价）与优惠券不可在一笔订单中同时使用",
    // 状态栏高度
    statusBarHeight: wx.getStorageSync('statusBarHeight') + 'px',
    statusBarPaddingTop: wx.getStorageSync('statusBarHeight') + 46 + 'px',
    // 导航栏高度
    navigationBarHeight: wx.getStorageSync('navigationBarHeight') + 'px',
    // 胶囊按钮高度
    menuButtonHeight: wx.getStorageSync('menuButtonHeight') + 'px',
    // 导航栏和状态栏高度
    navigationBarAndStatusBarHeight: wx.getStorageSync('statusBarHeight') +
      wx.getStorageSync('navigationBarHeight') +
      'px'
  },
  // 获取滚动条当前位置
  onPageScroll: function (e) {
    if (e.scrollTop > 100) {
      this.setData({
        floorstatus: true
      });
    } else {
      this.setData({
        floorstatus: false
      });
    }
  },
  messageclick:function(){
    console.log("跳转到消息页面")
    wx.navigateTo({
      url: '../my/message/message',
      
    })
  },
  //回到顶部
  goTop: function (e) { // 一键回到顶部
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {

    var result = await request("/category/list")
    console.log(result)
    this.setData({
      categoryList: result.data
    })

    var goodsListResult = await request("/goods/list?pageNum=" + this.data.pageNum + "&pageSize=" + this.data.pageSize)
    console.log(goodsListResult)
    this.setData({
      goodsList: goodsListResult.data.rows
    })






    const {
      statusBarHeight,
      platform
    } = wx.getSystemInfoSync()
    const {
      top,
      height
    } = wx.getMenuButtonBoundingClientRect()

    // 状态栏高度
    wx.setStorageSync('statusBarHeight', statusBarHeight)
    // 胶囊按钮高度 一般是32 如果获取不到就使用32
    wx.setStorageSync('menuButtonHeight', height ? height : 32)

    // 判断胶囊按钮信息是否成功获取
    if (top && top !== 0 && height && height !== 0) {
      const navigationBarHeight = (top - statusBarHeight) * 2 + height
      // 导航栏高度
      wx.setStorageSync('navigationBarHeight', navigationBarHeight)
    } else {
      wx.setStorageSync(
        'navigationBarHeight',
        platform === 'android' ? 48 : 40
      )
    }
    this.setData({
      isShowMask: false
    })
  },
  toGoodsDetail(e) {
    console.log(e)
    wx.navigateTo({
      url: '/pages/goodsInfo/goodsInfo?gid=' + e.currentTarget.dataset.gid,
    })
  },
  async addCartGoods(e) {
    let gid = e.currentTarget.dataset.gid;
    let userInfo = wx.getStorageSync('youxuan-user-userInfo')
    console.log(userInfo.id, gid)
    let result = await request("/cart/add/goods", {
      goodsId: gid,
      num: 1,
      userId: userInfo.id
    }, "POST")
    console.log(result)
    if (result.code == 200) {
      wx.showToast({
        title: '添加成功',
        icon: "none"
      })
    }
  },
  goSearchGoods(){
    wx.navigateTo({
      url: '/pages/searchGoods/searchGoods',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: async function () {
    this.setData({
      pageNum: this.data.pageNum + 1
    })
    var goodsListResult = await request("/goods/list?pageNum=" + this.data.pageNum + "&pageSize=" + this.data.pageSize)
    console.log('触底了', goodsListResult.data.rows.length)
    if (goodsListResult.data.rows.length > 0) {
      this.setData({
        goodsList: this.data.goodsList.concat(goodsListResult.data.rows)
      })
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})